package com.zm.campus.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zm.campus.config.sentinel.FuseDegradationBlockHandler;
import com.zm.campus.pojo.CommPointTh;
import com.zm.campus.pojo.RespBean;
import com.zm.campus.pojo.RespBeanEnum;
import com.zm.campus.query.TreeHoleQuery;
import com.zm.campus.service.ITreeHoleService;
import com.zm.campus.util.AssertUtil;
import com.zm.campus.vo.AuditListVo;
import com.zm.campus.vo.AuditTreeHoleVo;
import com.zm.campus.vo.LoginReturnVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zm
 */
@RestController
@RequestMapping("/treeHole")
public class TreeHoleController {
    @Autowired
    private ITreeHoleService treeHoleService;

    //获取所有树洞的列表信息
    @PostMapping("getAllTreeHole")
    public RespBean getAllTreeHole( @RequestBody TreeHoleQuery treeHoleQuery){
        return treeHoleService.getAllTreeHole(treeHoleQuery);
    }

    //管理员审核树洞
    @PostMapping("registerTreeHoleByIds")
    public RespBean registerTreeHole( AuditListVo auditListVo,Boolean isAudit,String role){//待审核的ids，是否通过
        return treeHoleService.registerTreeHole(auditListVo,isAudit,role);
    }
    //提交树洞审核
    @PostMapping("submitTreeHole")
    public RespBean submitTreeHole(@RequestBody AuditTreeHoleVo auditTreeHoleVo){
        return treeHoleService.submitTreeHole(auditTreeHoleVo);
    }

    //评论树洞
    @PostMapping("commentsTreeHole")
    public RespBean commentsTreeHole(@RequestBody CommPointTh commPointTh) throws IOException {
        return treeHoleService.commentsTreeHole(commPointTh);
    }

    //删除指定id的树洞
    @RequestMapping("deleteTreeHoleById")
    public RespBean deleteTreeHoleById(Long thId,String role){
        return treeHoleService.deleteTreeHoleById(thId,role);
    }

    //树洞点赞、取消点赞
    @PostMapping("addOrCancelGPoint")
    //指定blockHandlerClass兜底类blockHandler兜底方法。注意，这里的throws BlockException要加，否则会抛出UndeclaredThrowableException异常
    //因为抛出的异常是UndeclaredThrowableException 而这个异常不是 BlockException 的子类，无法被具体捕捉到
    @SentinelResource(value = "addOrCancelGPoint")
    public RespBean addOrCancelGPoint(Long thId,Boolean isPoint,String pointUserId)throws BlockException {//这里接收的是点赞或取消点赞的树洞的id
        return treeHoleService.addOrCancelGPoint(thId,isPoint,pointUserId);
    }

    //获取当前需要审核的树洞的数量
    @PostMapping("getNewAuditNums")
    public RespBean getNewAudit(String userId) {

        return treeHoleService.getNewAudit(userId);
    }

    //管理员获取所有待审核的树洞
    @PostMapping("getAllAudit")
    public RespBean getAllAudit(@RequestBody LoginReturnVo loginReturnVo, HttpServletRequest request){
        return  treeHoleService.getAllAudit(loginReturnVo,request);
    }

    //管理员修改树洞头像
    @PostMapping("updateTreeAvatar")
    public RespBean updateTreeAvatar(@RequestBody MultipartFile treeAvatarFile,String userId,HttpServletRequest request) throws IOException {
        AssertUtil.isTrue(StringUtils.isBlank(userId.trim()), RespBeanEnum.ReleaseUserNull);
      return   treeHoleService.updateTreeAvatar(treeAvatarFile,userId,request);
    }

    //获取当前登录用户所在学校的树洞的图片
    @PostMapping("getTreeAvatar")
    public RespBean getTreeAvatar(String userId){
        AssertUtil.isTrue(StringUtils.isBlank(userId.trim()), RespBeanEnum.ReleaseUserNull);
        return treeHoleService.getTreeAvatar(userId);
    }

    //获取指定用户的树洞新评论消息
    @PostMapping("getNewCommentByUid")
    public RespBean getNewCommentByUid(String userId){
        AssertUtil.isTrue(StringUtils.isBlank(userId.trim()), RespBeanEnum.ReleaseUserNull);
        return treeHoleService.getNewCommentByUid(userId);
    }


}
